package com.zmn.oms.admin.controller.work.modify;

import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.admin.controller.work.WorkBaseController;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderDutyConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.bo.order.OrderBO;
import com.zmn.oms.model.dto.work.modify.OrderWorkSuspendDTO;
import com.zmn.oms.model.vo.work.OperatePermissionVO;
import com.zmn.oms.model.vo.work.ZsNormalUpdateOrderVO;
import com.zmn.oms.zmn.business.interfaces.work.OrderWorkSuspendBService;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkBService;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkOperatePermissionBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Objects;

/**
 * 类描述：订单挂起
 *
 * @author haoguibin
 * @date 2020年07月24日15:31:49
 */
@Slf4j
@Controller
@RequestMapping("/work/modify")
public class WorkSuspendController extends WorkBaseController {

    @Autowired
    ZsOrderWorkOperatePermissionBService orderWorkOperatePermissionBService;
    @Autowired
    private ZsOrderWorkBService zsOrderWorkBService;
    @Autowired
    private OrderWorkSuspendBService orderWorkSuspendBService;

    /**
     * 挂起
     */
    @RequestMapping(value = "suspend")
    public ModelAndView suspend(Long orderId, Long workId) {
        ModelAndView mv = new ModelAndView("work/modify/suspend");

        OrderBO orderBO = orderWorkBService.findOrderWork(orderId, workId);
        ZsNormalUpdateOrderVO zsNormalUpdateOrderVO = zsOrderWorkBService.findUpdateOrderWorkVO(orderBO);
        // 订单
        mv.addObject("orderInfo", zsNormalUpdateOrderVO);
        // 预约失败原因
        mv.addObject("suspendList", super.dictModel2KVDict(OrderDutyConsts.getDutySuspendReasonList()));
        return mv;
    }

    /**
     * 挂起操作
     */
    @RequestMapping(value = "save/suspend")
    @ResponseBody
    public ResultDTO operateSuspend(@RequestBody OrderWorkSuspendDTO orderWorkSuspendDTO) {

        // 操作权限
        OperatePermissionVO operatePermission = orderWorkOperatePermissionBService.getOperatePermission(orderWorkSuspendDTO.getOrderId(), orderWorkSuspendDTO.getWorkId(), OrderConsts.ORDER_OP_TYPE_SUSPEND);
        if (!operatePermission.getCanOperate()) {
            return ResultDTO.fail(operatePermission.getMsg());
        }

        // 添加挂起备注
        String reasonDesc = orderWorkSuspendDTO.getReasonDesc();
        // 添加挂起原因
        Integer dutyFailCode = orderWorkSuspendDTO.getDutyFailCode();
        if (StringUtil.isBlank(reasonDesc) && Objects.isNull(dutyFailCode)) {
            return ResultDTO.fail("挂起原因/其他原因不能为空");
        }
        if (orderWorkSuspendDTO.getNextContactTime() == null) {
            return ResultDTO.fail("预约时间不能为空");
        }

        try {
            orderWorkSuspendBService.operateSuspend(orderWorkSuspendDTO);
        } catch (OmsBaseException e) {
            ResultDTO.fail(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ResultDTO.fail(e.getMessage());
        }
        return ResultDTO.success();
    }
}
